<template>
  <div class="attachment-container">
    <vab-query-form>
      <vab-query-form-top-panel v-show="!fold">
        <el-form ref="queryform" :inline="true" label-width="70px">
          <el-form-item label="文件名称">
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item label="储存位置">
            <el-select
              v-model="queryForm.storage"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in storgeOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="!fold">
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
            <el-button type="text" @click="fold = !fold">
              <span>合并</span>
              <vab-icon
                class="vab-dropdown"
                :class="{ 'vab-dropdown-active': fold }"
                icon="arrow-up-s-line"
              />
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :span="12">
        <!-- <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel v-if="fold" :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
            <el-button type="text" @click="fold = !fold">
              <span>展开</span>
              <vab-icon
                class="vab-dropdown"
                :class="{ 'vab-dropdown-active': fold }"
                icon="arrow-up-s-line"
              />
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
        width="55"
      />
      <el-table-column align="center" label="图片">
        <template #default="{ row }">
          <el-image
            v-if="imgShow"
            :preview-src-list="imageList"
            :src="row.fill_url"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="文件名称"
        prop="filename"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="宽x高" show-overflow-tooltip>
        <template #default="{ row }">
          <span>{{ row.imagewidth }} x {{ row.imageheight }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="大小"
        prop="filesize"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="类型"
        prop="mimetype"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="储存位置"
        prop="storage_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="上传时间"
        prop="created_at"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="操作" width="85">
        <template #default="{ row }">
          <el-button
            v-clipboard:copy="row.fill_url"
            v-clipboard:error="onCopyError"
            v-clipboard:success="onCopy"
            type="text"
          >
            复制
          </el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/attachment'
  import Edit from './components/AttachmentEdit'
  export default {
    name: 'Attachment',
    components: { Edit },
    data() {
      return {
        radio: '1',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
          storage: '',
        },
        imgShow: true,
        imageList: [],
        fold: true,
        storgeOption: [
          { value: 'public', label: '本地' },
          { value: 'qiniu', label: '七牛' },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        const { list, total } = data
        this.list = list
        this.total = total
        this.listLoading = false
      },
      onCopy() {
        this.$baseMessage('复制成功', 'success')
      },
      onCopyError() {
        this.$baseMessage('复制事变', 'error')
      },
    },
  }
</script>
<style scoped>
  .active {
    background-color: red;
  }
</style>
